Search this book | Previous | Table of contents | Next

Trapping and handling errors


This section demonstrates how to trap errors in your scripts.

Error trapping and handling is an imperative part of your CGI/ACGI scripting skills. Nothing is more frustrating to your readers than the meaningless message "Error executing script." Not only will they go away frustrated, but it will detract from your entire service. Furthermore, until AppleScripting becomes second nature to you, the lack of error trapping and handling will stunt your development skills. For these reasons, including error handling routines in your scripts is essential.

The way to trap and handle error messages in AppleScript CGI/ACGI programs is through the use of the try/on error/end try commands. What you want to do is wrap every critical routine in your script with try/on error/end try handler. Then, when an error occurs, an error message can be passed to the on error part of your handler and this handler returns valid HTML describing the error.

To demonstrate this process, a new version of the Name Game is used. As you may or may not have found out, in the previous versions of the Name Game script, if you entered a word that did not have any vowels (or a "y"), then the script returned the nasty error "Error executing script." In this new version of the Name Game (name-game-02.cgi), an error handler is included in the code. Try it. Run the Name Game again and enter a word containing no vowels and see what happens.

-- process the AppleEvent sent to this script by the server
on «event WWW*sdoc» given «class kfor»:http_search_args
	
	-- define the standard HTTP header
	set LF to ASCII character (10)
	set CR to return
	set CRLF to CR & LF
	set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
		"Server: MacHTTP" & CRLF & ¬
		"MIME-Version: 1.0" & CRLF & ¬
		"Content-type: text/html" & CRLF & CRLF
	
	-- practice good error trapping
	try
		
		if http_search_args = "" then
			
			-- http_search_args is empty; request input via ISINDEX
			return http_10_header & ¬
				"<html><head>" & ¬
				"<title>Play the Name Game</title><isindex></head>" & ¬
				"<body>" & ¬
				"<H1>Play the Name Game</H1>" & ¬
				"</body></html>"			
		else
			
			-- get our name
			set theName to word 1 of http_search_args as string
			
			-- initalize some variables for the next routine
			set foundVowel to 0
			set theCounter to 0
			set theSuffix to theName
			
			-- work through the name until we find the first vowel
			repeat until foundVowel is greater than 0
				
				-- increment our counter
				set theCounter to theCounter + 1
				
				-- get the next character
				set theCharacter to the first item of theSuffix
				
				-- check whether or not it is a vowel
				if "aeiouy" contains theCharacter or "AEIOUY" contains theCharacter then
					
					-- found it, exit
					set foundVowel to 1
					
				else
					
					-- left-hand truncate the name
					set theSuffix to items 2 through length of theSuffix as string
				end if
				
			end repeat
			
			-- compose the ryhmes
			set ryhmeOne to "B" & theSuffix
			set ryhmeTwo to "F" & theSuffix
			
			-- return the lyrics
			return http_10_header & ¬
				"<html><head>" & ¬
				"<title>The Name Game</title>" & ¬
				"<body>" & ¬
				"<h1>The Name Game</h1>" & ¬
				"<blockquote>" & theName & ", " & theName & ", Bo " & ryhmeOne & ".<br>" & return & ¬
				"Bananna fana, Fo " & ryhmeTwo & ".<br>" & return & ¬
				"Fe Fi Fo, " & ryhmeTwo & ".<br>" & return & ¬
				"<strong>" & theName & "</strong>!" & ¬
				"</body>" & ¬
				"</html>"			
		end if
		
	on error msg
		
		-- return the error as an HTML document
		return http_10_header & ¬
			"</html><head><title>Error</title></head>" & ¬
			"<body><h1>Error</h1>" & ¬
			"An error occured: " & msg & ¬
			"</body></html>"		
	end try
	
end «event WWW*sdoc»
The only additions to this script are the try/on error/end try sections. The real meat of the demonstration is in the on error routine. This routine is called when any sort of error occurs. If you don't supply a word containing a vowel, then the script is never able to left-hand truncate the name. This generates an error and is trapped by on error. On error receives one parameter, msg. This parameter is then incorporated into a simple HTML document and returned to the server.

There are two drawbacks to the script as it stands. First, if there is some sort of error in the on error routine, then your script will still return the nasty "Error executing script" message. All you can do here is just make sure there are no errors.

Second, you may want to wrap the try/on error/end try commands around many parts of your script. At the same time you don't want to write the same on error handler over and over again. The solution to this problem is to create a separate handler that only returns the marked up error message, and this handler can be called by the each on error handler. This is a three-step process.

  1. Declare your HTTP header as a global variable outside the WWW*sdoc handler. You will also want to define the header outside the WWW*sdoc handler. For example, at the beginning of your script, before the WWW*sdoc handler, insert the following code:
    -- declare the HTTP header as a global variable
    global http_10_header
    
    -- define the HTTP header as usual
    set LF to ASCII character (10)
    set CR to return
    set CRLF to CR & LF
    set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
    	"Server: MacHTTP" & CRLF & ¬
    	"MIME-Version: 1.0" & CRLF & ¬
    	"Content-type: text/html" & CRLF & CRLF
    
  2. Next, within your WWW*sdoc handler, wrap try/on error/end try statements around your code. Only this time, call a new handler within your on error statement. The purpose of this handler is to create the HTML error statement:
    -- practice good error handling
    try
    	-- your scripting routines go here
    on error msg
    	return ErrorMessage(msg)
    end try
  3. Finally, write the ErrorMessage handler. It could look something like this:
    on ErrorMessage(ErrorMessage)
    	
    	-- return the error as an HTML document
    	return http_10_header & ¬
    		"</html><head><title>Error</title></head>" & ¬
    		"<body><h1>Error</h1>" & ¬
    		"An error occured: " & ErrorMessage & ¬
    		"</body></html>"	
    end ErrorMessage

Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.